#include"../include/lib_headers.h"
char* create_vfs(char *vfs_path, int vfs_size){
	int i=0, size=0;
	char write='0' ;
	FILE *f=fopen(vfs_path, "rb");
	/* For create vfs the global header should not be used */
	hd *h_local = NULL;
	fd *array_local = NULL;
	int *freelist_local = NULL;

	/* Insuficient Arguments */
	if(strlen ( vfs_path ) == 0 || vfs_size == 0 ){
		sprintf(output_message,"%s %s","createvfs_FAILURE",ERR_VFS_CREATE_00);
		return output_message;
	}

	/* Data File already exists */
	if(f!=NULL){
		sprintf(output_message,"%s %s","createvfs_FAILURE",ERR_VFS_CREATE_01);	
		return output_message;
	}

	/* Invalid Character in name */	
	if(validate_name(vfs_path) == FAILURE){
		sprintf(output_message,"%s %s","createvfs_FAILURE",ERR_VFS_CREATE_03);
		return output_message;
	}

	
	/* Error if Maximun FD's is equal to zero */
	if((vfs_size * KB)/(BLOCKSIZE) == 0){
		sprintf(output_message,"%s %s","createvfs_FAILURE",ERR_VFS_CREATE_02);	
		remove(vfs_path);
		return output_message;
	}
	

	/* Invalid VFS size */
	if(vfs_size<=0 || vfs_size*KB > VFS_MAXIMUM_SIZE){
		sprintf(output_message,"%s %s","createvfs_FAILURE",ERR_VFS_CREATE_04);	
		return output_message;
	}

	/* VFS Label is too large */
	if(strlen(vfs_path) >30){
		sprintf(output_message,"%s %s","createvfs_FAILURE",ERR_VFS_CREATE_05);	
		return output_message;
	}

	/* cannot create data faile */
	FILE *fp=fopen(vfs_path,"wb");
	if(fp==NULL){
		sprintf(output_message,"%s %s","createvfs_FAILURE",ERR_VFS_CREATE_02);	
		return output_message;
	}
	
	size=sizeof(hd)+sizeof(fd)*((vfs_size * KB)/(BLOCKSIZE))+sizeof(int)*((vfs_size * KB)/(BLOCKSIZE))+vfs_size*(BLOCKSIZE);
	/* printf("size of vfs is :%d %d %d %d %d \n",sizeof(hd), sizeof(fd)*vfs_size, 
		sizeof(int)*vfs_size, vfs_size*1024, size); */

	if(fseek(fp,sizeof(hd),SEEK_SET)==-1){
		sprintf(output_message,"%s %s","createvfs_FAILURE",ERR_VFS_CREATE_02);	
		remove(vfs_path);		
		return output_message;	
	}
	if(fseek(fp,sizeof(fd)*((vfs_size * KB)/(BLOCKSIZE)),SEEK_CUR)==-1){
		sprintf(output_message,"%s %s","createvfs_FAILURE",ERR_VFS_CREATE_02);	
		remove(vfs_path);		
		return output_message;	
	}
	if(fseek(fp,sizeof(int)*((vfs_size * KB)/(BLOCKSIZE)),SEEK_CUR)==-1){
		sprintf(output_message,"%s %s","createvfs_FAILURE",ERR_VFS_CREATE_02);	
		remove(vfs_path);		
		return output_message;	
	}
	if(fseek(fp,(KB)*vfs_size-1,SEEK_CUR)==-1){
		sprintf(output_message,"%s %s","createvfs_FAILURE",ERR_VFS_CREATE_02);	
		remove(vfs_path);		
		return output_message;	
	}
	
	fwrite(&write,1,1,fp);
	
	/* To take to beginning of file */
	rewind(fp);
	
	/* Writing Header, FileDescriptor and Freelist  into the VFS for the first time */
	h_local=(hd*)malloc(sizeof(hd)); strcpy(h_local->VFS_Label, vfs_path); h_local->usedFds=0; h_local->maxFds=vfs_size;
	fwrite(h_local, sizeof(hd), 1, fp);
	
	/* Writing the fd array into the VFS */
	array_local=(fd*)malloc(sizeof(fd)*(h_local->maxFds));
	fwrite(array_local,sizeof(fd)*(h_local->maxFds), 1, fp);
	
	/* Writing the freelist into the VFS */
	freelist_local=(int*)calloc(sizeof(int),h_local->maxFds);
	fwrite(freelist_local, sizeof(int)*(h_local->maxFds), 1, fp);
	
	
/*	if(l1!=NULL)
 *		unmount_nAry(l1->next);
 *	if(bstRoot!=NULL)
 *		unmount_bst(bstRoot);
 *	unmount_hashTable();
 */	
 	/* Freeing the heap variables namely array_local, h_local anf freelistlocal */
	free(h_local);
	h_local = NULL;
	free(freelist_local);
	freelist_local = NULL;
	free(array_local);
	array_local=NULL; 
	/*Closing the file */
	fclose(fp);
	/* Printing the output message */
	sprintf(output_message,"%s","createvfs_SUCCESS");	
	return output_message;	
}
